package handler

import (
	"errors"
	"github.com/zeromicro/go-zero/rest/httpx"
	common "listenboos-common"
	"listenboos-common/tools"
	"live-api/internal/logic"
	"live-api/internal/svc"
	"net/http"
)

type LiveHandler struct {
	svcCtx *svc.ServiceContext
}

func (l *LiveHandler) FindAllLiveTag(w http.ResponseWriter, r *http.Request) {
	result := common.NewResult()
	live := logic.NewLiveLogic(r.Context(), l.svcCtx)
	res, err := live.FindAllLiveTag()
	if err != nil {
		result = result.Deal("未登录", errors.New("未登录"))
		httpx.WriteJson(w, 40001, result)
		return
	}
	result = result.Deal(res, nil)
	httpx.OkJsonCtx(r.Context(), w, result)
}

func (l *LiveHandler) GetById(w http.ResponseWriter, r *http.Request) {

}

func (l *LiveHandler) GetCurrentLive(w http.ResponseWriter, r *http.Request) {
	token := r.Header.Get("token")
	result := common.NewResult()
	userId, err := tools.ParseToken(token, l.svcCtx.Config.JWT.AccessSecret)
	if err != nil {
		result.Deal("未登录", errors.New("未登录"))
		httpx.WriteJson(w, 40001, result)
		return
	}
	live := logic.NewLiveLogic(r.Context(), l.svcCtx)
	res, err := live.GetCurrentLive(userId)
	if err != nil {
		result = result.Deal("未登录", errors.New("未登录"))
		httpx.WriteJson(w, 40001, result)
		return
	}

	result = result.Deal(res, nil)
	httpx.OkJsonCtx(r.Context(), w, result)
}

func NewLiveHandler(svcCtx *svc.ServiceContext) *LiveHandler {
	return &LiveHandler{
		svcCtx: svcCtx,
	}
}
